var centerTabs,systemNav,showDateTime,systemNavContextMenu,formUpdatePassw,dialogUpdatePassw,password,passwordNew,passwordRe;
var listNavAdmin,listNavPXJG,listNavJGJS;

function showTime() {
	var today = new Date();
	var weekday = new Array(7);
	weekday[0] = "星期一";
	weekday[1] = "星期二";
	weekday[2] = "星期三";
	weekday[3] = "星期四";
	weekday[4] = "星期五";
	weekday[5] = "星期六";
	weekday[6] = "星期日";
	var y = today.getFullYear() + "年";
	var month = today.getMonth() + 1 + "月";
	var td = today.getDate() + "日";
	var d = weekday[today.getDay()];
	var h = today.getHours();
	if (h >= 0 && h < 10) {
		h = "0"+h;
	}
	var m = today.getMinutes();
	if (m >= 0 && m < 10) {
		m = "0"+m;
	}
	var s = today.getSeconds();
	if (s >= 0 && s < 10) {
		s = "0"+s;
	}
	var str = y + month + td + d + "  " + h + ":" + m + ":" + s;
	showDateTime.text(str);
}

function timer(obj, txt) {
	obj.text(txt);
}

function addEasyTable(node) {
	if (node) {
		var title = node.text;
		var url = '';
		if (node.attributes) {
			url = node.attributes.url;
		}
		if (url) {
			$.messager.progress({text:'页面加载中。。。', interval:100});
			sy.addTab(sy.bp() + url, centerTabs, title);
		}
	}
};

function updatePassw(){
	password.val('');
	passwordNew.val('');
	passwordRe.val('');
	dialogUpdatePassw.show().dialog({
		title : '修改用户密码', 
		iconCls : 'icon-edit', 
		buttons : [{
			text : '确定', 
			handler : dialogEnterHandler
		}]
	}).dialog('open');
}

function dialogEnterHandler() {
	var url = sy.bp() + '/admin/user/updatePwd2';
	if (formUpdatePassw.form('validate')) {
		$.post(url, {"password":password.val(), "passwordNew":passwordNew.val(), "passwordRe":passwordRe.val()}, function(data){
			if(data && data.result ==1) {
				dialogUpdatePassw.dialog('close');
				$.messager.show({msg:data.msg,title:'系统提示'});
			} else {
				$.messager.alert('错误提示', data.msg);
			}
		},"json");
	}
}

function getDiff(DateTime, type){
	var today = new Date();
	var future = formatChinaDateTime(DateTime, type);
	var diff = future.getTime() - today.getTime();
	return diff;
};

function loadNav(listNavAdmin) {
	$.post(sy.bp() + "/admin/systemNavigation/" + listNavAdmin, function(data) {
		if (data && data.result==1) {
			systemNav.tree('loadData', data.obj);
		}
	}, "json");
}

$(function() {
	centerTabs = $('#centerTabs');
	systemNav = $('#systemNav');
	showDateTime = $('#showDateTime');
	systemNavContextMenu = $('#systemNavContextMenu');
	formUpdatePassw = $('#formUpdatePassw');
	dialogUpdatePassw = $('#dialogUpdatePassw');
	password = $('#password');
	passwordNew = $('#passwordNew');
	passwordRe = $('#passwordRe');
	listNavAdmin = $('#listNavAdmin').val();
	listNavPXJG = $('#listNavPXJG').val();
	listNavJGJS = $('#listNavJGJS').val();

	systemNav.tree({
		lines : false,
		onClick : function(node){
			if (node) {
				if (node.attributes) {
					addEasyTable(node);
				}
				if(node.state=='closed') {
					$(this).tree('expand',node.target);
				} else {
					$(this).tree('collapse',node.target);
				}
			} 
		},
		onContextMenu: function(e, node){
			e.preventDefault();
		},
		onBeforeExpand : function(node) {
			systemNav.tree('collapseAll');
			systemNav.tree('expandTo', node.target);
		}
	});
	
	$.post("loginVerified", function(data) {
		if (data && data.result == 1){
			if (data.obj == 'listNavAdmin') {
				if (listNavAdmin) {
					systemNav.tree('loadData', $.evalJSON(listNavAdmin));
				} else {
					loadNav(data.obj);
				}
			} else if (data.obj == 'listNavPXJG') {
				if (listNavPXJG) {
					systemNav.tree('loadData', $.evalJSON(listNavPXJG));
				} else {
					loadNav(data.obj);
				}
			} else if (data.obj == 'listNavJGJS') {
				if (listNavJGJS) {
					systemNav.tree('loadData', $.evalJSON(listNavJGJS));
				} else {
					loadNav(data.obj);
				}
			}
		} else {
			$.messager.alert('错误提示', data.msg);
		}
	}, "json");
	
	setInterval(showTime, 1000);
	
	centerTabs.tabs({
		fit : true,
		border : false,
		onContextMenu : function (e, title,index) {
			e.preventDefault();
			systemNavContextMenu.menu('show', {left:e.pageX,top:e.pageY}).data('tabTitle', title);
		}
	});
	
	systemNavContextMenu.menu({
		onClick : function(item) {
			var curTabTitle = $(this).data('tabTitle');
			var type = $(item.target).attr('title');
			if (type === 'refresh') {
				centerTabs.tabs('getTab', curTabTitle).panel('refresh');
				return;
			}
			if (type === 'close') {
				var t = centerTabs.tabs('getTab', curTabTitle);
				if (t.panel('options').closable) {
					centerTabs.tabs('close', curTabTitle);
				}
				return;
			}
			var allTabs = centerTabs.tabs('tabs');
			var closeTabsTitle = [];
			$.each(allTabs, function() {
				var opt = $(this).panel('options');
				if (opt.closable && opt.title != curTabTitle && type === 'closeOther') {
					closeTabsTitle.push(opt.title);
				} else if (opt.closable && type === 'closeAll') {
					closeTabsTitle.push(opt.title);
				}
			});
			for ( var i = 0; i < closeTabsTitle.length; i++) {
				centerTabs.tabs('close', closeTabsTitle[i]);
			}
		}
	});
});